# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
脚本类，用来对组合好的参数进行循环执行，依次带入组合参数请求。可加入对响应结果断言） 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests

from logger import Logger
from parameter.msydloanCs import parameter

logger = Logger("msydloan").get_log()
parameter = parameter()

url = 'https://test.zyjk.msxiaodai.com'

headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json'
        }

logger.info('-----------中移动项目接口测试开始---------')


def apply():
    logger.info('-----------授信申请接口测试开始---------')
    fu_url = url + '/cmpay/credit/v1/apply'
    result = []
    info = parameter.get_apply_data('../files/msydloan.xls')
    for data in info:
        rspCd = data[2]
        req1 = requests.post(fu_url, json=data[1], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            respond = eval(req1.text)['rspData']['rspCd']
            if respond != rspCd:
                logger.info("失败案例的授信发起信息：%s", data)
                logger.info("失败案例的授信响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    return result


def freeze():
    logger.info('-----------借款申请接口测试开始---------')
    fu_url = url + '/cmpay/quota/v1/freeze'
    result = []
    info = parameter.get_freeze_data('../files/msydloan.xls')
    for data in info:
        rspCd = data[2]
        req1 = requests.post(fu_url, json=data[1], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            respond = eval(req1.text)['rspData']['rspCd']
            if respond != rspCd:
                logger.info("失败案例的借款申请发起信息：%s", data)
                logger.info("失败案例的借款申请响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    return result


def block():
    logger.info('-----------套餐办理通知接口测试开始---------')
    fu_url = url + '/cmpay/quota/v1/block'
    result = []
    info = parameter.get_block_data('../files/msydloan.xls')
    for data in info:
        rspCd = data[2]
        req1 = requests.post(fu_url, json=data[1], headers=headers)
        fhcode = str(req1.status_code)
        if fhcode == '200':
            respond = eval(req1.text)['rspData']['rspCd']
            if respond != rspCd:
                logger.info("失败案例的借款申请发起信息：%s", data)
                logger.info("失败案例的借款申请响应信息：%s", req1.text)
                result.append('false')
            else:
                result.append('true')
        else:
            logger.info("http error info：%s", fhcode)
    return result


if __name__ == '__main__':
    block()